package com.heima.behavior.service.Impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.behavior.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zzy
 * @date 2022/7/28 11:58
 */

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Short operation = dto.getOperation();
        Long articleId = dto.getArticleId();
        //用户校验
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        Integer userId = user.getId();
        //判断操作类型
        if (operation == 0) {
            Object o = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + articleId, userId.toString());
            if (o != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "已点赞");
            }
            long millis = System.currentTimeMillis();
            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + articleId, userId.toString(), Long.toString(millis));
        } else if (operation == 1) {
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + articleId, userId.toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "数据不存在在");
        }
        Integer userId = user.getId();
        Long articleId = dto.getArticleId();

        String o = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString());
        if (o != null) {
            //获取阅读数量
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(o, ReadBehaviorDto.class);
            Short count = readBehaviorDto.getCount();
            Short count1 = dto.getCount();
            Short i = (short) (count + count1);
            dto.setCount(i);
        }
        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString(), JSON.toJSONString(dto));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Long articleId = dto.getArticleId();
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userId = user.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "数据不存在");
        }
        Short type = dto.getType();
        //0-不喜欢  1-取消喜欢
        if (type == 0) {
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId, userId.toString(), JSON.toJSONString(dto));
        }
        cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId, userId.toString());
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
